package com.rjwh.dingdong.client.activity.community;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.aishua.lib.utils.AsStrUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout.OnRefreshListener;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.adapter.CommunityPostMsgAdapter;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.TopicNotice;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.mvp.presenter.impl.CommunityPostMsgPresenter;
import com.rjwh.dingdong.client.mvp.view.CommunityPostMsgView;
import com.rjwh.dingdong.client.util.DeviceUuidFactory;
import com.rjwh.dingdong.client.util.LogUtil;
import com.wby.utility.AppUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 社区消息
 *
 * @author 王小珂
 * @ClassName CommunityPostMsgActivity
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2016-4-14 下午4:44:13
 * @history 1.YYYY-MM-DD author: description:
 */
public class CommunityPostMsgActivity extends BaseActivity implements CommunityPostMsgView {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.community_post_msg_listview)
    ListView communityPostMsgListview;
    @BindView(R.id.community_post_msg_mPullRefreshView)
    SwipyRefreshLayout mSwipyRefreshLayout;

    private CommunityPostMsgAdapter adapter;
    private CommunityPostMsgPresenter presenter;

    protected static final int REQUESTCODE = 123;
    public static final int RESULTCODE = 1;
    private int pageNumber = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.community_post_msg_layout);
        ButterKnife.bind(this);
        initView();
        presenter = new CommunityPostMsgPresenter(this, this);
        mSwipyRefreshLayout.doPullRefreshing();
    }


    /**
     * 获取帖子消息
     *
     * @param isRefresh 是否刷新
     */
    private void doGetTopicNotice(boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        presenter.doGetTopicNotice(pageNumber, isRefresh, direction);
    }

    private void initView() {
        initToolbarHelper();
        mSwipyRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                switch (direction) {
                    case TOP:
                        pageNumber = 0;
                        doGetTopicNotice(true, SwipyRefreshLayoutDirection.TOP);
                        break;

                    case BOTTOM:
                        pageNumber++;
                        doGetTopicNotice(false, SwipyRefreshLayoutDirection.BOTTOM);
                        break;

                    default:
                        break;
                }
            }
        });
        adapter = new CommunityPostMsgAdapter(this);
        communityPostMsgListview.setAdapter(adapter);
        communityPostMsgListview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                TopicNotice item = (TopicNotice) arg0.getAdapter().getItem(arg2);
                item.setIsnew("0");
                MyApplication.spUtil.setStrPreference(MyApplication.app, LocalConstant.SP_COMMUNITY_MSG_UNREAD, "0");
                String link = item.getFilepath();
                Intent intent = new Intent(CommunityPostMsgActivity.this, CommunityPostDetailActivity.class);
                String url = "";
                if (!AsStrUtils.isEmpty(link)) {
                    url = link + "?userid=" + MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_USERID) + "&platform=ANDROID" + "&version=" + AppUtil.GetVersionCode(getApplicationContext()) + "&udid=" + DeviceUuidFactory.getUUID(getApplicationContext()) + "&babyid=" + MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_BABYID) + "&token=" + MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_USER_TOKEN);
                }
                LogUtil.e("CommunityPostMsgActivity", "拼接的url: " + url);
                intent.putExtra("url", url);
                intent.putExtra("topicid", item.getTopicid());
                startActivityForResult(intent, REQUESTCODE);
            }
        });
    }

    /**
     * init the toolbar
     */
    protected void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        getSupportActionBar().setTitle("社区消息");
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(1);
                CommunityPostMsgActivity.this.finish();
            }
        });
    }

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        super.onActivityResult(arg0, arg1, arg2);
        if (arg0 == REQUESTCODE && arg1 == RESULT_OK) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void setDirection(SwipyRefreshLayoutDirection direction) {
        mSwipyRefreshLayout.setDirection(direction);
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        mSwipyRefreshLayout.setRefreshing(refreshing);
    }

    @Override
    public void loadData(boolean isRefresh, List<TopicNotice> noticelist) {
        if (isRefresh) {
            adapter.clear();
        }
        adapter.appendToList(noticelist);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            setResult(RESULTCODE);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        presenter.detachView();
        super.onDestroy();
    }

}
